﻿namespace Coding4Fun.Cannon.Robot.Types
{
	public class TemperatureReturn
	{
		public TemperatureReturn(int[] data)
		{
			ThermistorA = CalcTemp(data[data.Length - 1]);
			ThermistorB = CalcTemp(data[data.Length - 2]);
		}

		public double ThermistorA
		{
			get;
			set;
		}

		public double ThermistorB
		{
			get;
			set;
		}

		// Interpolation table. Analog readings at -40 to 150 oC, in 5o intervals
		private static readonly int[] TempTable ={248, 246, 243, 240, 235, 230, 224, 217, 208, 199, 188, 177,
		                                   165, 153, 140, 128, 116, 104, 93, 83, 74, 65, 58, 51, 45, 40, 35, 31, 27, 24, 21,
		                                   19, 17, 15, 13, 12, 11, 9, 8};

		// formula based off page 60, refactored to reduce var creation
		private int CalcTemp(int value)
		{
			int i = TempTable.Length - 1;

			// CER: todo: improve this loop
			while (TempTable[i] < value && i > 0)
				i--;

			if (i == 38)
				return 150;

			return (i * 5 - 40) + (5 * ((value - TempTable[i]) * 100 / (TempTable[i + 1] - TempTable[i]))) / 100;
		}
	}
}


